package cloud.lysky.java.on.concurrent.thread;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author luoly
 * @describe
 * @date 2022-10-18 17:46
 **/
public class ThreadDemo implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        return new Random().nextInt(10);
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //提交任务，并用Future提交返回结果
        Future<Integer> future = executorService.submit(new ThreadDemo());
        System.out.println("获得结果：" + future.get());
    }
}

//        //创建线程对象
//        Thread thread = new Thread(){
//            @Override
//            public void run() {
//                //要执行的任务
//                System.out.println("创建了新线程" + Thread.currentThread().getName());
//            }
//        };
//        //启动线程
//        thread.start();

//-----------------------------------------------------

//        //创建任务
//        Runnable runnable = new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("创建了新线程" + Thread.currentThread().getName());
//            }
//        };
//        //启动线程
//        new Thread(runnable).start();